//
//  ALITradeDemoImageLoader.m
//  NBUltimateDemo
//
//

#import "ALITradeDemoImageLoader.h"
#import <SDWebImage/SDWebImageDownloader.h>
#import <SDWebImage/UIImageView+WebCache.h>

@implementation ALITradeDemoImageLoader

- (id<WMLImageOperationProtocol>)downloadImageWithURL:(NSURL *)imageUrl
                                                frame:(CGRect)imageFrame
                                              options:(NSDictionary *)options
                                             progress:(void(^)(NSInteger receivedSize, NSInteger expectedSize))progressBlock
                                            completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock {
    NSInteger imageQuality = [[options objectForKey:@"imageQuality"] integerValue];
    SDWebImageDownloader*imageLoader = [SDWebImageDownloader new];
    id<SDWebImageOperation> obj =[imageLoader downloadImageWithURL:imageUrl options:imageQuality progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (completedBlock) {
            completedBlock(image, error, finished);
        }
     }];
    
    return (id<WMLImageOperationProtocol>)obj;
}

- (void)setImageView:(UIImageView *)imageView
             withURL:(NSURL *)imageUrl
         placeholder:(UIImage *)placeholder
             options:(NSDictionary *)options
            progress:(void(^)(NSInteger receivedSize, NSInteger expectedSize))progressBlock
           completed:(void(^)(UIImage *image, NSError *error, WMLImageLoaderCacheType cacheType, NSURL *imageURL))completedBlock {
    NSInteger imageQuality = [[options objectForKey:@"imageQuality"] integerValue];
    [imageView sd_setImageWithURL:imageUrl placeholderImage:placeholder options:(SDWebImageOptions)imageQuality progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        WMLImageLoaderCacheType type = WMLImageLoaderCacheTypeNone;
        if (cacheType == SDImageCacheTypeMemory) {
            type = WMLImageLoaderCacheTypeMemory;
        } else if (cacheType == SDImageCacheTypeDisk) {
            type = WMLImageLoaderCacheTypeDisk;
        }
        if (completedBlock) {
            completedBlock(image, error, type, imageUrl);
        }
    }];
}

@end
